package com.szh.zx.gaoDingJavaBook.chapter8.three;

import java.util.TreeSet;

class R2 implements Comparable{
	int count;

	public R2(int count) {
		this.count = count;
	}

	@Override
	public String toString() {
		return "R2{" +
				"count=" + count +
				'}';
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj){
			return true;
		}
		if (obj != null && obj.getClass()==R.class){
			R2 r = (R2) obj;
			return r.count == this.count;
		}
		return false;
	}
// 重写compareTo（）方法，根据count来比较大小
	@Override
	public int compareTo(Object o) {
		R2 r = (R2) o;
		return count > r.count ? 1: count<r.count ? -1: 0;
	}
}
public class TreeSetTest3 {
	public static void main(String[] args) {
		TreeSet treeSet = new TreeSet();
		treeSet.add(new R2(5));
		treeSet.add(new R2(-3));
		treeSet.add(new R2(9));
		treeSet.add(new R2(-2));
//		打印Treeset集合，集合元素是有序排列的
		System.out.println(treeSet);
//		取出第一个元素

		R2 first = (R2) treeSet.first();
//		对第一个元素赋值
		first.count = 20;
//		取出最后一个元素
		R2 last = (R2) treeSet.last();
//		对最后一个元素的count赋值，与第二个元素的count相同
		last.count = -2;
//
		System.out.println("两次修改之后的元素排列："+treeSet);
//		删除实例变量被改变的元素，删除失败
		System.out.println(treeSet.remove(new R2(-2)));
		System.out.println("删除实例变量修改的值后："+treeSet);
		System.out.println(treeSet.remove(new R2(5)));
		System.out.println("删除实例变量不改变的值后："+treeSet);

	}
}
